package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment;
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment2;
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment3;
import com.yanggeng.demo.beginnerguidedemo.widget.CustomPager;
import android.support.v4.view.ScalePagerStrip;
import com.yanggeng.demo.beginnerguidedemo.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.BezierPagerIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanggeng on 2017/11/7.
 */

public class ViewPagerActivity extends BaseActivity {

    private MagicIndicator magicIndicator;
    private ViewPager viewPager;
    private ScalePagerStrip pagerTabStrip;
    private List<String> titles = new ArrayList<String>();
    private List<Integer> categoryIdList = new ArrayList<Integer>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private CustomPager pager1, pager2;

    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        context = this;
        magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        pagerTabStrip = (ScalePagerStrip) findViewById(R.id.pagerTabStrip);
        titles.add("2015");
        titles.add("2016");
        titles.add("2017");
        titles.add("2015");
        titles.add("2016");
        titles.add("2017");
        initViewPager();
//        initCustomPager();
    }

    private void initIndicator() {

//        titles.add("2018");
//        titles.add("2019");
//        titles.add("2020");
//        titles.add("2021");
        CommonNavigator commonNavigator = new CommonNavigator(this);
//        commonNavigator.setAdjustMode(true);

        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int i) {
                ScaleTransitionPagerTitleView colorTransitionPagerTitleView = new ScaleTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setText(titles.get(i));
                colorTransitionPagerTitleView.setTextSize(18);
                colorTransitionPagerTitleView.setNormalColor(Color.GRAY);
                colorTransitionPagerTitleView.setSelectedColor(Color.BLACK);
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(i);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
//                List<String> colorList = new ArrayList<String>();
//                colorList.add("#ff4a42");
//                colorList.add("#fcde64");
//                colorList.add("#73e8f4");
//                colorList.add("#76b0ff");
//                colorList.add("#c683fe");
//                indicator.setColorList(colorList);

                indicator.setColors(Color.parseColor("#000000"));

                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);


        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    private void initViewPager() {
//        pagerTabStrip.setTabIndicatorColor(Color.BLUE);
//        pagerTabStrip.setDrawFullUnderline(false);
        fragmentList.add(new TestFragment());
        fragmentList.add(new TestFragment2());
        fragmentList.add(new TestFragment3());
        fragmentList.add(new TestFragment());
        fragmentList.add(new TestFragment2());
        fragmentList.add(new TestFragment3());
//        fragmentList.add(new TestFragment4());
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });

        pagerTabStrip.setViewPager(viewPager);

//        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//                if (state == ViewPager.SCROLL_STATE_DRAGGING) {
//
//                }
//            }
//        });
    }

    private void initCustomPager() {
        pager1 = (CustomPager) findViewById(R.id.pager1);
        pager2 = (CustomPager) findViewById(R.id.pager2);

        pager1.setViewPager(pager2);
        pager2.setViewPager(pager1);

        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = View.inflate(context, R.layout.c_item_home_active, null);
                ImageView imgItem = (ImageView) view.findViewById(R.id.img_item);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        };

        pager1.setAdapter(pagerAdapter);
        pager2.setAdapter(pagerAdapter);

        pager1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < pager1.getChildCount(); i ++) {
                    View view = pager1.getChildAt(i);
                    if (i == position) {
                        view.findViewById(R.id.iv_icon).setVisibility(View.VISIBLE);
                    } else {
                        view.findViewById(R.id.iv_icon).setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
